package com.xfatm.killer.entity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xfatm.killer.R;
import com.xfatm.killer.task.DisableAllAppTask;
import com.xfatm.killer.task.StartAppTask;
import com.xfatm.killer.util.Activitys;

import java.util.List;
import java.util.Set;


/**
 * Created by wenjie on 2018/3/24.
 */

public class SelectAppInfoAdapter extends BaseAdapter implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{

    private List<AppInfo> appInfos;
    private Context context;

    public SelectAppInfoAdapter(Context context, List<AppInfo> data){
        this.context=context;
        this.appInfos=data;
    }

    @Override
    public int getCount() {
        return appInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return appInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return appInfos.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.select_app_grid_item,parent,false);
            holder = new ViewHolder();
            holder.appName = (TextView) convertView.findViewById(R.id.selectAppName);
            holder.icon=convertView.findViewById(R.id.selectAppIcon);
            convertView.setTag(holder);   //将Holder存储到convertView中
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.appName.setText(appInfos.get(position).getAppName());
        holder.icon.setImageDrawable(appInfos.get(position).getIcon());
        //是否启用显示颜色
        if(appInfos.get(position).isEnable()){
            holder.appName.setTextColor(Color.BLACK);
        }else{
            holder.appName.setTextColor(convertView.getContext().getResources().getColor(R.color.colorPrimaryDark));
        }
        return convertView;
    }

    static class ViewHolder{
        TextView appName;
        ImageView icon;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //打开app
        AppInfo appInfo=appInfos.get(position);
        /*if(!appInfo.isEnable()){
            AppInfoController.getInstance().enableApp(appInfo);
        }
        PackageManager pm= Activitys.top().getPackageManager();
        Intent intent=pm.getLaunchIntentForPackage(appInfo.getPackageName());
        Activitys.top().startActivity(intent);*/
        if(!appInfo.isEnable()){
            TextView textView=Activitys.top().findViewById(R.id.disableText);
            ProgressBar progressBar=Activitys.top().findViewById(R.id.disableProgress);
            textView.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
        }
        new StartAppTask(appInfo,Activitys.top()).execute();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        AppInfo appInfo=appInfos.get(position);
        if(appInfo.isEnable()){
            AppInfoController.getInstance().disableApp(appInfo);
        }
        Toast.makeText(view.getContext(),"已禁用",Toast.LENGTH_SHORT).show();
        return true;
    }
}
